package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbUser;
import com.guigu.qingcheng.service.TbUserService;
import com.guigu.qingcheng.pojo.vo.NewuserVo;
import com.guigu.qingcheng.utils.MD5;
import com.guigu.qingcheng.utils.PageUtil;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
*
* @author Jiang
* @date 2020/08/05
*/
@Slf4j
@RestController
@RequestMapping("/user/user")
@Api(tags = "TbUserController")
public class TbUserController {
    @Autowired
    private TbUserService tbUserService;

    @ApiOperation(value = "查询新增")
    @PostMapping("/findnewuser")
    public R findnewuser(@RequestBody NewuserVo newuserVo) {
        Map newuser = tbUserService.findnewuser(newuserVo);
        return R.ok().data("newuser",newuser.get("newuser"));
    }

    @ApiOperation(value = "查询所有会员")
    @GetMapping("/finduser")
    public R finduser() {
        Map userLevel = tbUserService.finduser();
        return R.ok().data("userLevel",userLevel.get("sumuser"));
    }

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {

        TbUser tbUser = tbUserService.findById(id);
        return R.ok().data("tbUser",tbUser);
    }
    @GetMapping("/findById")
    public R findById(String username){
        TbUser tbUser = tbUserService.findById(username);
        return R.ok().data("tbUser",tbUser);
    }

//    @ApiOperation(value = "根据用户名获取信息")
//    @GetMapping("/queryUsername/{username}")
//    public R details(@PathVariable("username") String username) {
//
//        TbUser TbUserone = tbUserService.findByUsername(username);
//        return R.ok().data("TbUserone",TbUserone);
//    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public R list() {
        List<TbUser>  list = tbUserService.queryAll();
        return R.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages/{pageNum}/{pageSize}")
    public R listPages(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageUtil<TbUser> pageUtils = tbUserService.findListAll(pageNum, pageSize);

        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public R pageQueryByAll(@RequestBody TbUser tbUser, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbUser> pageUtils = tbUserService.pageQueryByAll(pageNum, pageSize, tbUser);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public R add(@RequestBody TbUser tbUser) {

        boolean b = tbUserService.saveTbUser(tbUser);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R update(@RequestBody TbUser tbUser) {
        if (!StringUtils.isEmpty(tbUser.getPassword())){
            tbUser.setPassword(MD5.encrypt(tbUser.getPassword()));
        }
        boolean b = tbUserService.updateTbUser(tbUser);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{username}")
    public R delete(@PathVariable("username") String username) {
        boolean b = tbUserService.deleteById(username);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    /**
     * 发送短信验证码
     * @param phone
     */
    @GetMapping(value="/sendSms")
    public R sendSms(String phone){
        tbUserService.sendSms(phone);
        return R.ok().message("验证码发送成功");
    }

    /**
     * 用户注册
     * @param tbUser
     * @param smsCode
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody TbUser tbUser , String smsCode ){
        //密码加密
        String newpassword =  MD5.encrypt(tbUser.getPassword());
        tbUser.setPassword(newpassword);
        tbUserService.add(tbUser,smsCode);
        return R.ok().message("注册成功");
    }
    // 登录
    @PostMapping("/login")
    public R loginUser(@RequestBody TbUser tbUser) {
        // 调用service方法实现登录
        // 返回token值，使用jwt生成
        String token = tbUserService.login(tbUser);
        System.out.println("token==========="+token);
        if (token=="手机号或者密码不正确！！！"||token=="手机号或者密码为空！！！"){
            return R.error().data("token",token);
        }
        return  R.ok().data("token",token);
    }

    // 根据token获取用户信息
    @GetMapping("/getUserInfo")
    public R getUserInfo(@RequestParam String username) {
        // 查询数据库根据用户id获取用户信息
        TbUser tbUser = tbUserService.findById(username);
        return  R.ok().data("tbUser",tbUser);
    }

    @ApiOperation(value = "通过用户名修改密码")
    @PostMapping("/updatePasswordByUsername")
    public R updatePasswordByUsername(@RequestBody TbUser user) {
        tbUserService.updatePasswordByUsername(user);
        return R.ok();
    }
    //测试密码
    public static void main(String[] args){
        System.out.println(MD5.encrypt("111111"));
    }
}
